package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.ui.base.BaseUIActivity;
import com.ouertech.android.agm.lib.ui.base.UIOptions;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.WishProduct;
import com.simon.mengkou.data.cache.DataCacheImpl;
import com.simon.mengkou.data.enums.EDirection;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.adapter.HotSaleAdapter;
import com.simon.mengkou.ui.adapter.WishListAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xiazhenshui on 16/7/24.
 * 热销榜单界面
 */

public class HotSaleActivity extends BaseUIActivity{
    @Bind(R.id.common_pulltorefresh_id_list)
    PullToRefreshListView mPtrView;

    @Bind(R.id.hot_sale_id_num)
    TextView mTvNum;

    private HotSaleAdapter mAdapter;

    @Override
    protected long initOptions() {
        return UIOptions.UI_OPTIONS_SYSTEMBAR
                | UIOptions.UI_OPTIONS_APPBAR_CUSTIOM
                | UIOptions.UI_OPTIONS_CONTENT_CUSTOM;
    }


    @Override
    protected void initAppCustom() {
        setAppCustomLayout(R.layout.layout_hot_sale_header);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.layout_common_pulltorefresh_list);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        registerAction(CstOuer.BROADCAST_ACTION.ADD_CART_ACTION);

        int num = OuerApplication.mPreferences.getCartProductNum();
        if(num == 0) {
            mTvNum.setVisibility(View.GONE);
        } else {
            mTvNum.setVisibility(View.VISIBLE);
            mTvNum.setText(String.valueOf(num));
        }

        mPtrView.setMode(PullToRefreshBase.Mode.DISABLED);
        mPtrView.setTag(false);

        mAdapter = new HotSaleAdapter(this);
        mPtrView.setAdapter(mAdapter);


        //获取缓存
        List<WishProduct> datas = OuerApplication.mCacheFactory
                .getCache(DataCacheImpl.HotSaleCache.class)
                .getData();

        if(UtilList.isNotEmpty(datas)) {
            mAdapter.setList(datas);
            mPtrView.setTag(true);
        } else {
            mPtrView.setTag(false);
            //首次失败重试
            setOnRetryListener(new OnRetryListener() {
                @Override
                public void onRetry() {
                    getData(0, EDirection.REFRESH);
                }
            });
        }

        //获取数据
        getData(0, EDirection.REFRESH);
        cartProductNum();
    }

    @Override
    protected void onReceive(Intent intent) {
        super.onReceive(intent);
        String action = intent.getAction();
        if(CstOuer.BROADCAST_ACTION.ADD_CART_ACTION.equals(action)) {
            int num = intent.getIntExtra(CstOuer.KEY.CART_NUM, OuerApplication.mPreferences.getCartProductNum());
            if(num == 0) {
                mTvNum.setVisibility(View.GONE);
            } else {
                mTvNum.setVisibility(View.VISIBLE);
                mTvNum.setText(String.valueOf(num));
            }
        }
    }

    /**
     * 获取新粉丝消息
     * @param page
     * @param direction
     */
    private void getData(final int page, final int direction) {
        AgnettyFuture future = OuerApplication.mOuerFuture.hotSales(
                page,
                100,
                new OuerFutureListener(this){

                    public void onStart(AgnettyResult result) {
                        boolean flag = (Boolean)mPtrView.getTag();
                        if(!flag) {
                            setLoading(true);
                        }
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        boolean flag = (Boolean)mPtrView.getTag();
                        if(!flag) {
                            mPtrView.setTag(true);
                            setLoading(false);
                        }

                        List<WishProduct> datas = (List<WishProduct>) result.getAttach();

                        OuerApplication.mCacheFactory.getCache(DataCacheImpl.HotSaleCache.class)
                                .save(datas);

                        if(UtilList.isEmpty(datas)) {
                            setEmptyText(R.string.hot_sale_empty);
                            mAdapter.clearAll();
                        } else {
                            mAdapter.setList(datas);
                        }
                    }

                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        boolean flag = (Boolean)mPtrView.getTag();
                        if(!flag) {
                            setRetry(true);
                        }
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        boolean flag = (Boolean)mPtrView.getTag();
                        if(!flag) {
                            setRetry(true);
                        }
                    }
                });

        attachDestroyFutures(future);
    }

    /**
     * 获取购物车商品数量
     */
    private void cartProductNum() {
        AgnettyFuture future = OuerApplication.mOuerFuture.cartProductNum(null);
        attachDestroyFutures(future);
    }

    @OnClick(R.id.user_id_setting)
    public void goCart() {
        OuerDispatcher.presentCartActivity(this);
    }

    @OnClick(R.id.hot_sale_id_nav)
    public void goNav() {
        finish();
    }
}
